1 Restarting work

1.1 Session

Restart session either by using GUI Session > Restart R or by Ctrl + Shift + F10. You should start with empty “Environment”. Tip: you can also clear your R console with Ctrl + L.

1.2 Packages

Load packages:

if (!require("tidyverse")) install.packages("tidyverse"); library("tidyverse")
if (!require("sf")) install.packages("sf"); library("sf")
if (!require("tmap")) install.packages("tmap"); library("tmap")
if (!require("tmaptools")) install.packages("tmaptools"); library("tmaptools")
if (!require("sjmisc")) install.packages("sjmisc"); library("sjmisc")

1.3 Data

Load data prepared during previous session:

SA2_SEIFA <- readRDS("data/SA2_SEIFA.Rds")

2 Mapping SEIFA indices in Melbourne - interactive maps

2.1 Quick map

We already know how to use quck map qtm functionality of tmap library. We will extend its use by providing additional argument from linked SEIFA data:

tmap_mode("view")
SA2_SEIFA %>% 
  qtm(fill = "IRSAD_d")

We get one more map of SA2s. This time it’s a thematic map! We mapped IRSAD_d variable to assign a colour that will fill polygon of SA2 using its particular value of decile.

2.2 Improving interactive map

Making quick maps can quickly become cumbersome when more options need to be specified to improve the map. We will switch to building maps layer by layer, similarly to ggplot package. Lets rewrite the code using more advanced syntax of tmap. We first define our base map using tm_shape function and then instruct tmap to treat it as layers of polygons with certain attributes:

tm_shape(SA2_SEIFA) +
  tm_polygons(col = "IRSAD_d", 
              n = 10, alpha = 0.7, palette = "RdYlGn", lwd = 0)

Can you guess what the arguments of tm_polygons function are? Use help(tm_polygons) to learn about the one that you don’t know. Try changing a colour scheme of your map. Running code tmaptools::palette_explorer() will help guide your choices.

2.3 Faceted maps

Sometimes it might be useful to explore two variables at once. tmap lets you create facets (in a similar way as ggplot does) using tm_facets. We can create linked interactive display of two variables:

tm_shape(SA2_SEIFA) +
  tm_polygons(col = c("IRSAD_d", "IER_d"), 
              n = 10, alpha = 0.7, palette = "RdYlGn", lwd = 0) +
  tm_facets(sync = TRUE, ncol = 2)

Pay attention that the col argumant consists now of two variables combines using function call c("IRSAD_d", "IER_d").

2.4 Proportional symbol map

Variables can be mapped not only to colours, but also to size. We replace tm_polygons call with tm_bubbles to get a proportional symbol map:

tm_shape(SA2_SEIFA) +
  tm_borders(lwd = 0.5) +
  tm_bubbles(col = "IRSAD_d", size = "URP", 
             n = 10, alpha = 0.7, palette = "RdYlGn", border.lwd = 0, scale = 0.5)

Note that the size argument has been mapped to URP variable that contains values of usual resident population in the area. In this way - the size of bubbles is now determinded by population. Can you think of advantages of such map? Are there any locations where it can be useful?

3 Mapping SEIFA indices in Melbourne - static maps

Interactive maps are great for exploration. On other occasions ‘static’ map can be a better option, for instance when you want to include it in a printed report. We will learn how to create one with slightly more advanced use of tmap library functions.

3.1 Quick map

First we change mode of tmap:

tmap_mode("plot")

Now we can once again create a choropleth map:

tm_shape(SA2_SEIFA) +
  tm_polygons("IRSAD_d", title = "IRSAD deciles", 
              palette = "RdYlGn", n = 10, lwd = 0) 

Notice that the arguments are the same as in interactive mode.

3.2 Improving static map

Our map is already starting to look good. We can improve some aspects of design by adding and arranging items of the layout such as scale bar, north arrow and creating histogram of data according to classes used for mapping:

tm_shape(SA2_SEIFA) +
  tm_polygons("IRSAD_d", title = "IRSAD deciles", 
              palette = "RdYlGn", n = 10, lwd = 0,
              legend.hist = T) +
  tm_scale_bar(position = c("right", "bottom")) +
  tm_compass(position = c("left", "top"))+
  tm_layout(frame = F, 
            main.title = "IRSAD deciles of SA2 areas in Melbourne",
            main.title.size = 1.25, 
            main.title.position = c("left", "top"),
            legend.hist.size = 0.5, 
            legend.outside = T)

We have built a map layer by layer, calling different functions and specifying various arguments. We also improved the cartography by providing legend and a histogram (a graph of distribution of values). We specified our data source in tm_shape() function; many arguments indicating how polygons are displayed are specified in tm_polygons() function; we also added north arrow and scale with functions names appropriately; finally tm_layout helped us to put all small details together as legend and title).

Last but not least we saved our map can be saved as file on the disk. That can be done with a call tmap_save(tmap_last(), "path_to_save/name.png"). Notice that we used function inside a finction there to use last displayed map.

4 Further topics

  1. Try mapping indices other than IRSAD.

  2. Try creating static map with proportianl symbols.

  3. Try creating static maps with facets.